package com.mmall.controller.admin;

import com.mmall.commons.Const;
import com.mmall.commons.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.mmall.util.CheckUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author wgt
 * @date 2018/6/27
 * @project mmall-server
 */
@Controller
@RequestMapping("/manage/user")
public class ManagerUserController {

    @Autowired
    private IUserService userService;

    /**
     * 管理员登陆
     * @param username
     * @param password
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public ServerResponse login(String username, String password, HttpSession session){
        ServerResponse<User> serverResponse = userService.login(username, password);

        if (serverResponse.isSuccess()){
            User user = serverResponse.getData();
            ServerResponse checkResponse = CheckUserUtil.checkRoleAdmin(user);
            if (checkResponse.isSuccess()){
                session.setAttribute(Const.CURRENT_USER, user);
                return serverResponse;
            }
            return ServerResponse.createByErrorMessage("不是管理员,无法登陆");
        }

        return serverResponse;
    }
}
